#include "top.h"



char nums[10][10];


//任意键返回
void press_any_key(void)
{
         printf("\n请按任意键进入下一内容...\n");
        //关闭行缓冲功能和回显
        system("stty -icanon -echo");
        getchar();
        //恢复终端行缓冲和回显
        system("stty icanon echo");
}



void cover(void)
{
	system("clear");
	printf("\n\n欢迎来到五子棋对战\n\n\n");
	printf("请选择对战模式：\n");
	printf("1. 玩家 VS 玩家\n");
	printf("2. 玩家 VS 人机\n");
	printf("0. 退出\n");

//	press_any_key();

}


void map()
{
	system("clear");
	printf("                    五子棋\n                玩家  vs  玩家\n\n");
	for(int i = 0;i < 10; i++)
	{
		for(int q = 0; q < 10; q++)
		{
			if(i == 0)
			{
				printf("    %d",q);
				
			}

		}
		printf("\n");
		printf("%d   ",i);
		for(int j = 0; j < 10; j++)
		{
			nums[i][j] = '.';
			printf("%c    ",nums[i][j]);
		}
		printf("\n");
	}
	mo();
}




//玩家 VS 玩家
void mo()
{
	while(1)
	{
		int m, n;
		char c;
		printf("\n按 q 退出\n请卧龙落子(先输入行)：");

		getchar();
		c = getchar();

		if(c == 'q')
		{
			break;
		}
		else
		{
			ungetc(c, stdin);
		}

		if(scanf("%d%d",&m, &n) != 2)
		{
			while(getchar() != '\n');
			m = -1;
			n = -1;
		}
		if(m < 0 || m >= 10 || n < 0 || n >= 10)
		{
			system("clear");
			p();
			printf("\n坐标超出范围！请输入0-9之间的数字。");
			continue;
		}

		if(nums[m][n] != '.')
		{
			system("clear");
			p();
			printf("\n该位置已有棋子！请选择其他位置。");
			continue;
		}
		nums[m][n] = 'o';
		p();
		if(win() == 'o' || win() == 'x')
		{
			printf("\nWin！！！\n\n\n");
			press_any_key();
			break;
		}
tip1:		printf("\n按 q 退出\n请凤雏落子(先输入行)：");

		getchar();
		c = getchar();
		if(c == 'q')
		{
			break;
		}
		else
		{
			ungetc(c, stdin);
		}
		if(!scanf("%d%d",&m, &n))
		{
			while(getchar() != '\n');
			m = -1;
			n = -1;
		}
		if(m < 0 || m >= 10 || n < 0 || n >= 10)
		{
			system("clear");
			p();
			printf("\n坐标超出范围！请输入0-9之间的数字。");
			goto tip1;
		}

		if(nums[m][n] != '.')
		{
			system("clear");
			p();
			printf("\n该位置已有棋子！请选择其他位置。");
			goto tip1;
		}

		nums[m][n] = 'x';
		p();
		if(win() == 'o' || win() == 'x')
		{
			printf("\nWin！！！\n\n\n");
			press_any_key();
			break;
		}	
	}
}



//遍历打印棋盘
void p()
{
	system("clear");
	printf("                    五子棋对战\n\n\n");
	for(int i = 0;i < 10; i++)
	{
		for(int q = 0; q < 10; q++)
		{
			if(i == 0)
			{
				printf("    %d",q);

			}

		}
		printf("\n");
		printf("%d   ",i);
		for(int j = 0; j < 10; j++)
		{
			printf("%c    ",nums[i][j]);
		}
		printf("\n");

	}
}


//人机对战棋盘
void map2()
{
        system("clear");
        printf("                    五子棋\n                玩家  vs  人机\n\n");
        for(int i = 0;i < 10; i++)
        {
                for(int q = 0; q < 10; q++)
                {
                        if(i == 0)
                        {
                                printf("    %d",q);

                        }

                }
                printf("\n");
                printf("%d   ",i);
                for(int j = 0; j < 10; j++)
                {
                        nums[i][j] = '.';
                        printf("%c    ",nums[i][j]);
                }
                printf("\n");
        }
        pva();
}




//人机对战
void pva()
{
	/*
	   srand(time(NULL));
	   int t = (rand() % 2) + 1;
	   */
	int q = 0;

	while(1)
	{
		int m, n;
		char c;
		printf("\n请按 q 退出\n请卧龙落子(先输入行)：");
		getchar();	
		c = getchar();
		if(c == 'q')
		{
			break;
		}
		else
		{
			ungetc(c, stdin);
		}
		if(!scanf("%d%d",&m, &n))
		{
			while(getchar() != '\n');
			m = -1;
			n = -1;
		}	
		if(m < 0 || m >= 10 || n < 0 || n >= 10)
		{
			system("clear");
			p();
			printf("\n坐标超出范围！请输入0-9之间的数字。");
			continue;
		}

		if(nums[m][n] != '.')
		{
			system("clear");
			p();
			printf("\n该位置已有棋子！请选择其他位置。");
			continue;
		}

		nums[m][n] = 'o';
		p();
		if(win() == 'o' || win() == 'x')
		{
			printf("\nWin！！！\n\n\n");
			getchar();
			press_any_key();
			break;
                }


		if(q == 0)
		{
			for(int i = 1; i < 8; i++)
			{
				if(nums[m + i][n] != 'x' && nums[m+i][n] != 'o')
				{
					nums[m+i][n] = 'x';
					p();
					break;
				}
			}
		}
		else if(q == 1)
		{                        
			for(int i = 1; i < 8; i++)
			{       
				if(nums[m + i][n+i] != 'x' && nums[m+i][n+i] != 'o')
				{
					nums[m+i][n+i] = 'x';
					p();
					break;
				}
			}

		}
		else if(q == 2)
		{                        
			for(int i = 1; i < 8; i++)
			{       
				if(nums[m][n + i] != 'x' && nums[m][n +  i] != 'o')
				{
					nums[m][n+i] = 'x';
					p();
					break;
				}
			}

		}
		else if(q == 3)
		{
			for(int i = 1; i < 8; i++)
			{
				if(nums[m-i][n-i] != 'x' && nums[m-i][n-i] != 'o')
				{
					nums[m-i][n-i] = 'x';
					p();
					break;
				}
			}

		}
		else
		{
			for(int i = 1; i < 8; i++)
			{
				if(nums[m][n-i] != 'x' && nums[m][n-i] != 'o')
				{
					nums[m][n-i] = 'x';
					p();
					break;
				}
			}

			q = -1;
		}
		 if(win() == 'o' || win() == 'x')
                {
                        printf("\ndefeat！！！\n\n\n");
			getchar();
                        press_any_key();
                        break;
                }

		q++;
	}
}



//判断赢
char win()
{
	for(int i = 0; i < 10; i++)
	{
		for(int j = 0; j < 10; j++)
		{
			if((nums[i][j] == 'o'||nums[i][j] == 'x') && (nums[i][j] == nums[i+1][j] &&
			   nums[i][j] == nums[i+2][j] &&
			   nums[i][j] == nums[i+3][j] &&
			   nums[i][j] == nums[i+4][j] ))
			{
			return nums[i][j];
			}
		}
	}
	for(int i = 0; i < 10; i++)
	{
		for(int j = 0; j < 10; j++)
		{
			if((nums[i][j] == 'o'||nums[i][j] == 'x') && (nums[i][j] == nums[i][j+1] &&
			   nums[i][j] == nums[i][j+2] &&
			   nums[i][j] == nums[i][j+3] &&
			   nums[i][j] == nums[i][j+4] ))
			return nums[i][j];
		}
	}
	for(int i = 0; i < 10; i++)
	{
		for(int j = 0; j < 10; j++)
		{
			if((nums[i][j] == 'o'||nums[i][j] == 'x') && (nums[i][j] == nums[i+1][j+1] &&
			   nums[i][j] == nums[i+2][j+2] &&
			   nums[i][j] == nums[i+3][j+3] &&
			   nums[i][j] == nums[i+4][j+4] ))
			return nums[i][j];
		}
	}
	for(int i = 0; i < 10; i++)
	{
		for(int j = 0; j < 10; j++)
		{
			if((nums[i][j] == 'o'|| nums[i][j] == 'x') && (nums[i][j] == nums[i+1][j-1] &&
			   nums[i][j] == nums[i+2][j-2] &&
			   nums[i][j] == nums[i+3][j-3] &&
			   nums[i][j] == nums[i+4][j-4] ))
			return nums[i][j];
		}
	}
	return '.';
}


/*

int main()
{
	map();
	return 0;
}
*/
